//
//  UserCollectViewController.m
//  Fengly_Nuo_Movie
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 Fengly_Nuo. All rights reserved.
//

#import "UserCollectViewController.h"
#import "Fengly_nuo_SQLDataBaseHandle.h"
#import "ActivityParticularsViewController.h"
#import "MovieParticularsViewController.h"
#import "ActivityModel.h"
#import "ActivityParticularsViewController.h"
#import "MovieParticularsViewController.h"

@interface UserCollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@end

@implementation UserCollectViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    // 创建tableView视图
    [self createTableView];
}



#pragma mark - tabelView 编辑方法
// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    // 首先执行父类的编辑按方法
    [super setEditing:editing animated:animated];
    // 设置tablev的编辑状态
    [_tableView setEditing:editing animated:animated];
}

// 设置哪一行可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// 设置cell 编辑的模式（默认是删除）
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 全部设置为可删除
    return UITableViewCellEditingStyleDelete;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"取消收藏";
}

// 实现编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // 判断为删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 如果是 我的活动
        if (!_b) {
            [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] deleteActivityModel:_arrData[indexPath.row]];
            // 如果是 我的电影
        } else if (_b) {
            [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] deleteMovieModel:_arrData[indexPath.row]];
        }
        // 删除数组中的元素
        [_arrData removeObjectAtIndex:indexPath.row];
        // 从tableView中 按位置删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)];
        
    }
}

#pragma mark - tabelView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.textLabel.text = _arrData[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 判断为我的活动
    if (!_b) {
        ActivityParticularsViewController *APVC = [[ActivityParticularsViewController alloc] init];
        NSString *titleString = _arrData[indexPath.row];
        ActivityModel *model = [[Fengly_nuo_SQLDataBaseHandle sharedDataBaseHandle] selectActivity:titleString];
        APVC.model = model;
        [self.navigationController pushViewController:APVC animated:YES];
    } else if (_b) {
        MovieParticularsViewController *MPVC = [[MovieParticularsViewController alloc] init];
        [self.navigationController pushViewController:MPVC animated:YES];
    }
    
}




/** 创建tableView视图 */
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 允许tableView进入编辑状态
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
